<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            list-style: none;
        }
        
        .div {
            width: 13%;
            margin: 100px auto;
        }
        
        .div li {
            margin-top: 20px;
        }
        
        .div input {
            border: none;
            border: transparent;
            background: #F4F5F5;
            display: inline-block;
            width: 200px;
            line-height: 36px;
            border-radius: 20px;
            margin-left: 25px;
            padding-left: 21px;
            outline: none;
        }
        
        .c0,
        .c1,
        .c2 {
            margin-left: 10px;
            height: 20px;
            overflow: hidden;
        }
        
        .c2 {
            margin-left: 10px;
            height: 20px;
            overflow: hidden;
        }
        
        .c11 {
            margin-left: 10px;
        }
        
        img {
            width: 20px;
            transform: rotateZ(0deg);
        }
        
        .act {
            transform: rotateZ(90deg);
        }
        
        .lo {
            width: 20px;
            transform: rotateZ(0deg);
        }
        
        .active {
            padding: 11px;
            color: red;
            margin-left: 10px;
            height: 20px;
            background: #EAF5FF;
            overflow: hidden;
            border-radius: 5px;
        }
    </style>
</head>

<body>
    <div class="div">

        <input type="text" class="val" placeholder="搜索">
        <ul class="box">
        </ul>
    </div>

    <script src="data.js"></script>
    <script>
        function $(el) {
            return document.querySelector(el)
        };

        function $s(el) {
            return [...document.querySelectorAll(el)]
        };

        function create(datas, el, num) { //渲染封装
            let index = num
            datas.forEach(item => {
                let newli = document.createElement("li")
                newli.className = `c${index}`
                newli.dataset.flag = 0
                if (item.indexGl <= 1) {
                    newli.innerHTML = `<img src='https://i.52112.com/icon/256/20190121/28511/1412563.png' />` +
                        item.name

                } else {
                    newli.innerHTML = item.name
                    newli.style.marginLeft = '30px'
                }

                if (item.child) {
                    create(item.child, newli, num + 1)
                }
                el.appendChild(newli);


            });
        }
        create(data, $('.box'), 0)

        $('.box').onclick = (e) => { //点击层级
            if (e.target.dataset.flag == 0) {

                e.target.dataset.flag = 1
                e.target.className = "c11"
                    // console.log(e.target.childNodes[0]);
                e.target.childNodes[0].className = 'act'

            } else {

                e.target.className = "c2"
                e.target.dataset.flag = 0
                e.target.childNodes[0].className = 'lo'
            }
        }

        function findName(arr, name) {

            function findNameChild(arr, name) {

                for (let i = 0; i < arr.length; i++) {
                    if (arr[i].name === name) {
                        return {
                            indexGl: arr[i].indexGl,
                            name
                        }
                    } else if (arr[i].child) {
                        let y = findName(arr[i].child, name)
                        return y
                    }
                }

            }
            return findNameChild(arr, name)
        }

        $(".val").onblur = (e) => {
            let val = $(".val").value
            let result = findName(data, val)
            let dom = $s(".box li")
            dom[result.indexGl].className = 'active'
                // console.log(dom[result.indexGl].parentNode);
                // console.log(result.indexGl);
            if (result.indexGl == 0) {
                dom[result.indexGl].className = 'active'

            } else if (result.indexGl == 1) {
                dom[result.indexGl].className = 'active'
                dom[result.indexGl].parentNode.className = 'c11'
            } else if (result.indexGl == 2 || result.indexGl == 3) {
                dom[result.indexGl].className = 'active'
                dom[result.indexGl].parentNode.className = 'c11'
                dom[result.indexGl].parentNode.parentNode.className = 'c11'
            }

        }
    </script>
</body>

</html>

</html>